﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using ITProject.Repository.Abstract;
using ITProject.Repository.Concrete;
using ITProject.Domain.Entities;

namespace SVNClient
{
    /// <summary>
    /// Interaction logic for LoginControl.xaml
    /// </summary>
    public partial class LoginControl : UserControl
    {
        IUserRepository _userRepository;

        public LoginControl()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (_userRepository == null)
            {
                _userRepository = new UserRepository();
            }

            string salt = "asdhahsdjnjasdhjk";
            var sha512 = new SHA512Managed();
            byte[] plainPasswordbytes = Encoding.UTF8.GetBytes(string.Concat(passwordBox1.Password, salt));
            var EncryptedBytes = sha512.ComputeHash(plainPasswordbytes);
            StringBuilder s = new StringBuilder();
            foreach (byte b in EncryptedBytes)
                s.Append(((char)b).ToString());
            User user = _userRepository.GetUserByLogin(textBox1.Text);
            if (user == null)
            {
                return;
            }
            if (s.ToString() != user.Password)
            {
                return;
            }
            MainWindow win = (MainWindow)Window.GetWindow(this.Parent);
            win.RootContent.Children.Clear();
            win.RootContent.Children.Add(new SelectProjectControl(textBox1.Text));
            win.login = textBox1.Text;
        }
    }
}
